const int maxPulses = 100;
unsigned char outputPin = 5;
unsigned int pulseWidths[maxPulses][2];

double outputFreq = 40000;
double outputPeriod = (1 / outputFreq) * 1000000;
//double outputHalfPeriod = ((outputPeriod - 6) / 2);
double outputHalfPeriod = ((outputPeriod) / 2);

int outputHalfPeriodDelay = (unsigned int)outputHalfPeriod;

void setup()
{
  Serial.begin(115200);
  Serial.println("Starting...");
  Serial.print("Freq = ");
  Serial.print(outputFreq, DEC);
  Serial.println(" Hz");
  Serial.print("Period = ");
  Serial.print(outputPeriod, DEC);
  Serial.println(" uSecs");
  Serial.print("Half Period Delay = ");
  Serial.print(outputHalfPeriodDelay, DEC);
  Serial.println(" uSecs");
  
  pinMode(outputPin, OUTPUT);
  digitalWrite(outputPin, HIGH);
  
  /*
  pulseWidths[0][0] = 1000;
  pulseWidths[0][1] = 2000;
  pulseWidths[1][0] = 3000;
  pulseWidths[1][1] = 4000;
  pulseWidths[2][0] = 5000;
  pulseWidths[2][1] = 6000;
  pulseWidths[3][0] = 7000;
  pulseWidths[3][1] = 8000;
  pulseWidths[4][0] = 9000;
  pulseWidths[4][1] = 10000;
  pulseWidths[5][0] = 11000;
  pulseWidths[5][1] = 12000;
  */
  
//Vizio Power Button  
pulseWidths[0][0] = 9084;
pulseWidths[0][1] = 4416;
pulseWidths[1][0] = 628;
pulseWidths[1][1] = 492;
pulseWidths[2][0] = 632;
pulseWidths[2][1] = 492;
pulseWidths[3][0] = 628;
pulseWidths[3][1] = 1612;
pulseWidths[4][0] = 632;
pulseWidths[4][1] = 492;
pulseWidths[5][0] = 628;
pulseWidths[5][1] = 492;
pulseWidths[6][0] = 632;
pulseWidths[6][1] = 492;
pulseWidths[7][0] = 628;
pulseWidths[7][1] = 492;
pulseWidths[8][0] = 632;
pulseWidths[8][1] = 492;
pulseWidths[9][0] = 628;
pulseWidths[9][1] = 1612;
pulseWidths[10][0] = 632;
pulseWidths[10][1] = 1612;
pulseWidths[11][0] = 628;
pulseWidths[11][1] = 492;
pulseWidths[12][0] = 632;
pulseWidths[12][1] = 1612;
pulseWidths[13][0] = 628;
pulseWidths[13][1] = 1612;
pulseWidths[14][0] = 632;
pulseWidths[14][1] = 1612;
pulseWidths[15][0] = 628;
pulseWidths[15][1] = 1612;
pulseWidths[16][0] = 632;
pulseWidths[16][1] = 1612;
pulseWidths[17][0] = 628;
pulseWidths[17][1] = 492;
pulseWidths[18][0] = 632;
pulseWidths[18][1] = 492;
pulseWidths[19][0] = 628;
pulseWidths[19][1] = 492;
pulseWidths[20][0] = 632;
pulseWidths[20][1] = 1612;
pulseWidths[21][0] = 628;
pulseWidths[21][1] = 492;
pulseWidths[22][0] = 632;
pulseWidths[22][1] = 492;
pulseWidths[23][0] = 628;
pulseWidths[23][1] = 496;
pulseWidths[24][0] = 628;
pulseWidths[24][1] = 492;
pulseWidths[25][0] = 628;
pulseWidths[25][1] = 1616;
pulseWidths[26][0] = 628;
pulseWidths[26][1] = 1612;
pulseWidths[27][0] = 628;
pulseWidths[27][1] = 1612;
pulseWidths[28][0] = 632;
pulseWidths[28][1] = 492;
pulseWidths[29][0] = 628;
pulseWidths[29][1] = 1612;
pulseWidths[30][0] = 632;
pulseWidths[30][1] = 1612;
pulseWidths[31][0] = 628;
pulseWidths[31][1] = 1612;
pulseWidths[32][0] = 632;
pulseWidths[32][1] = 1612;
pulseWidths[33][0] = 628;
pulseWidths[33][1] = 39864;
pulseWidths[34][0] = 9084;
pulseWidths[34][1] = 2168;
pulseWidths[35][0] = 628;
pulseWidths[35][1] = 96184;
pulseWidths[36][0] = 9084;
pulseWidths[36][1] = 2168;
pulseWidths[37][0] = 628;
pulseWidths[37][1] = 0;
  
  
  
  
  
}

void loop()
{
  sendPulseTrain(pulseWidths, 36);
  //printPulseTrain(pulseWidths, 3);
  delay(5000);
}

void sendPulseTrain(unsigned int widths[][2], unsigned char length)
{  
  for(unsigned int i=0; i<length; i++)
  {
    unsigned long modPulses = (unsigned long) ( (double)widths[i][0] / (double)outputPeriod );
    
    /*
    Serial.print("Sending ");
    Serial.print(modPulses, DEC);
    Serial.print(" Pulses and ");
    Serial.print(widths[i][1], DEC);
    Serial.println(" uSec Delay");
    */
    
    //Start One Pulse
    for(int j=0; j<modPulses; j++)
    {
      // Active Time
      //digitalWrite(outputPin, LOW);     // ~3uS 
      PORTD &= 0b11011111;
      
      delayMicroseconds(outputHalfPeriodDelay);
      //digitalWrite(outputPin, HIGH);     // ~3uS 
      PORTD |= 0b00100000;
      delayMicroseconds(outputHalfPeriodDelay);   
    }
    //Idle Time
    delayMicroseconds(widths[i][1]);
  }  
}


void printPulseTrain(unsigned int widths[][2], unsigned char length)
{
    for(unsigned int i=0; i<length; i++)
    {
      Serial.print("Sending ");
      Serial.print(widths[i][0], DEC);
      Serial.print(" uSec High and ");
      Serial.print(widths[i][1], DEC);
      Serial.println(" uSec Low");
    }
}
